﻿using UnityEngine;
using System.Collections;

public class CollisionSample : MonoBehaviour {

    public Color onColor;
    public Color offColor;
    public SpriteRenderer spriteToChange;
	public bool noShape;

	public ColorLens cLens;
    private GameObject lastGameObject;

    public float scaleSpeed;

    public bool returnToScale;
    private bool isScaleChange;
    void Update()
    {
        if (!isScaleChange)
        {
            if(transform.localScale.x < 1)
            transform.localScale = new Vector3(transform.localScale.x + (scaleSpeed * Time.deltaTime), transform.localScale.y + (scaleSpeed * Time.deltaTime), transform.localScale.z);

        }

        if (transform.localScale.x < 0)
            Destroy(gameObject);

    	onColor = cLens.GetComponent<SpriteRenderer>().color;
        if (lastGameObject != null)
        {
            if (lastGameObject.active == false)
            {
				spriteToChange.color = offColor;
				rigidbody2D.gravityScale = 0f;
				rigidbody2D.velocity = new Vector2(0, 0);
				if (!noShape)
				{

				}
                isScaleChange = false;
				lastGameObject = null;
                returnToScale = true;
			}
			if (lastGameObject.GetComponent<Vision>().justFlipped)
			{
				spriteToChange.color = offColor;
				rigidbody2D.gravityScale = 0f;
				rigidbody2D.velocity = new Vector2(0, 0);
				if (!noShape)
				{

				}
				lastGameObject = null;
                returnToScale = true;
			}
        }


    }

	void OnTriggerStay2D(Collider2D collider)
	{
		if (collider.tag == "Vision")
		{
			spriteToChange.color = onColor;

			switch(collider.GetComponent<Vision>().visionType)
			{
			case Vision.VisionType.gravDown:
                    isScaleChange = false;
				rigidbody2D.gravityScale = 0.3f;
				if (!noShape)
				{

				}
				break;
			case Vision.VisionType.gravUp:
                isScaleChange = false;
				rigidbody2D.gravityScale = -0.3f;
				if (!noShape)
				{

				}
				break;
			case Vision.VisionType.shape:
				//change shape;
                isScaleChange = true;
                returnToScale = false;
				rigidbody2D.gravityScale = 0f;
				rigidbody2D.velocity = new Vector2(0, 0);
				
				if (noShape)
				{
					break;
				}

                transform.localScale = new Vector3(transform.localScale.x - (scaleSpeed * Time.deltaTime), transform.localScale.y - (scaleSpeed * Time.deltaTime), transform.localScale.z);
				break;
				
			}
			lastGameObject = collider.gameObject;
		}
	}

    void OnTriggerExit2D(Collider2D collider)
    {
        if (collider.tag == "Vision")
        {
			spriteToChange.color = offColor;
            rigidbody2D.gravityScale = 0f;
			rigidbody2D.velocity = new Vector2(0, 0);
			if (!noShape)
			{

            }
            isScaleChange = false;

            lastGameObject = null;
            returnToScale = true;
        }
    }
}
